package com.ruoyi.web.controller.creditlife;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.creditlife.domain.ComplianceRecord;
import com.ruoyi.creditlife.domain.CreditLifeBehavior;
import com.ruoyi.creditlife.service.IComplianceRecordService;
import com.ruoyi.creditlife.service.ICreditLifeBehaviorService;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/creditlife/behavior")
public class CreditLifeBehaviorController extends BaseController
{
    @Autowired
    private ICreditLifeBehaviorService creditLifeBehaviorService;

    @Autowired
    private IComplianceRecordService complianceRecordService;

    @Autowired
    private TokenService tokenService;

    @PreAuthorize("@ss.hasPermi('creditlife:behavior')")
    @GetMapping("/list")
    public TableDataInfo list(CreditLifeBehavior creditLifeBehavior) {
        startPage();
        creditLifeBehavior.setUserId(SecurityUtils.getUserId());
        List<CreditLifeBehavior> list = creditLifeBehaviorService.selectCreditLifeBehaviorList(creditLifeBehavior);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('creditlife:behavior')")
    @Log(title = "信用生活行为记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CreditLifeBehavior creditLifeBehavior) {
        List<CreditLifeBehavior> list = creditLifeBehaviorService.selectCreditLifeBehaviorList(creditLifeBehavior);
        ExcelUtil<CreditLifeBehavior> util = new ExcelUtil<CreditLifeBehavior>(CreditLifeBehavior.class);
        util.exportExcel(response, list, "信用生活行为记录数据");
    }

    @PreAuthorize("@ss.hasPermi('creditlife:behavior')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(creditLifeBehaviorService.selectCreditLifeBehaviorById(id));
    }

    @PreAuthorize("@ss.hasPermi('creditlife:behavior')")
    @Log(title = "信用生活行为记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CreditLifeBehavior creditLifeBehavior) {
        return toAjax(creditLifeBehaviorService.insertCreditLifeBehavior(creditLifeBehavior));
    }

    @PreAuthorize("@ss.hasPermi('creditlife:behavior')")
    @Log(title = "信用生活行为记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CreditLifeBehavior creditLifeBehavior) {
        creditLifeBehavior.setCreditImpact((long)10);
        creditLifeBehavior.setEndTime(new Date());
        creditLifeBehavior.setStatus("已完成");
        creditLifeBehaviorService.updateCreditLifeBehavior(creditLifeBehavior);

        ComplianceRecord complianceRecord = new ComplianceRecord();
        complianceRecord.setUserId(creditLifeBehavior.getUserId());
        complianceRecord.setRecordType((long)0);
        complianceRecord.setDescription("便捷泊车");
        complianceRecord.setAmount(creditLifeBehavior.getAmount());
        complianceRecord.setDueTime(new Date());
        complianceRecord.setCompleteTime(new Date());
        complianceRecord.setStatus("已完成");
        complianceRecord.setCreditImpact(creditLifeBehavior.getCreditImpact());

        creditLifeBehaviorService.ImpactCreditScore(creditLifeBehavior.getUserId(), 10);
        return toAjax(complianceRecordService.insertComplianceRecord(complianceRecord));
    }

    @PreAuthorize("@ss.hasPermi('creditlife:behavior')")
    @Log(title = "信用生活行为记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(creditLifeBehaviorService.deleteCreditLifeBehaviorByIds(ids));
    }
}